package com.action.app_noverify;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.enetiy.Areainfo;
import com.enetiy.Areaip;
import com.enetiy.Binding;
import com.enetiy.Userinfo;
import com.operation.HotspotLink;
import com.service.AreaIpService;
import com.service.AreainfoService;
import com.service.BindingService;
import com.service.UserinfoService;
import com.util.Log4gUtil;

public class AppAction {
	private UserinfoService  userinfoService;
	private BindingService bindingService;
	private AreaIpService areaIpService;
	private AreainfoService areainfoService;
	private int num;
	
	/**
	 * 
	 * 缓存token
	 */
	public void cacheToken(){
		try {
			ServletContext application =  ServletActionContext.getRequest().getSession().getServletContext();
			List<Userinfo> userinfos = userinfoService.selectUserinfos();
			if(!userinfos.isEmpty()){
				for (int i = 0; i <userinfos.size(); i++) {
					application.setAttribute(userinfos.get(i).getUsers().getId()+"", userinfos.get(i).getToken());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * 更新绑定表数据
	 */
	public void updateBindingData(){
		if(num>0){
			try {
				Areainfo teacher = areainfoService.selectAreainfo(2, "NF-net-00");
				Areaip selectAreaip = areaIpService.selectAreaip(2);
				for (int i = 0; i < num; i++) {
					Binding binding = bindingService.selectBinding(i);
					if(binding!=null){
						HotspotLink hl = new HotspotLink();
						int result = hl.hotspotVerify(binding.getAccount(), binding.getAccountPwd(), 2, binding.getAreainfo().getVerifyUrl(), selectAreaip.getPublicIp(), binding.getUsers().getId(), "", "");
						if(result==1){
							if(hl.type.equals("老师")){
								binding.setTypeExplain(hl.type);
								binding.setAreainfo(teacher);
								bindingService.updateBinding(binding);
							}
						}else{
							i=i-1;
						}
						Thread.sleep(500);
						Log4gUtil.getLog().info("num+++++++++++++:"+i);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		Log4gUtil.Info("updateBindingData", "", "结果", "结束", "", "");
	}
	
	
	
	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}
	public void setBindingService(BindingService bindingService) {
		this.bindingService = bindingService;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public void setAreaIpService(AreaIpService areaIpService) {
		this.areaIpService = areaIpService;
	}
	public void setAreainfoService(AreainfoService areainfoService) {
		this.areainfoService = areainfoService;
	}
	
}
